// 引入接口文件
import {getLiveBaseStatistics, getLineChart, getBarChart, getAreaChart,getUserPosition,getUserPosition2} from './liveStatisticsServ.js'

// 对象和JSON字符串互转
import {parse} from 'qs'
// 弹出消息框
import {message} from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import {hashHistory} from 'dva/router'
import config from 'config/config';
import {download} from 'utils/util'
import {makeAddress} from "utils/util"
import {detailServ} from "pages/marketingAdmin/liveAdmin/liveDetail/liveDetailServ";

// 初始默认状态
const defultState = {
    exportLoading: false,
    loading: false,
    areaList: [],
    checkBtn: true,
    btnLoading: false,
    actionType: '',
    biz: {
        queryForm: {}
    },
    modalObj: {
        visible: false,
    },
    codeList: {},//验证码返回数据
    codeQuery: {},//验证码
    provinceData:[],
    markers:[],
    province:null,
    center:{}
}

let formatSeconds = function (value) {

    let seconds = parseInt(value);
    let minutes = 0;
    let hours = 0;
    let result = "";

    if (seconds > 60) {
        minutes = parseInt(seconds / 60);
        seconds = parseInt(seconds % 60);
        if (minutes > 60) {
            hours = parseInt(minutes / 60);
            minutes = parseInt(minutes % 60);
        }
    }

    if (seconds > 0) {
        result = parseInt(seconds) + "秒";
    }
    if (minutes > 0) {
        result = "" + parseInt(minutes) + "分" + result;
    }
    if (hours > 0) {
        result = "" + parseInt(hours) + "小时" + result;
    }

    return result;
};

// 声明module
const tmpModule = {
    // 默认状态
    state: cloneDeep(defultState),
    // 入口函数(先执行)
    subscriptions: {
        setup({dispatch, history}) {
            history.listen(({pathname, query}) => {
                let {actionType, id} = query
                // 页面路由匹配时执行
                if ('' + tmpModule.pathname === '' + pathname) {
                    dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
                    dispatch({type: 'updateStore', payload: {actionType}});

                    dispatch({type: 'getLiveBaseStatistics', payload: {outerId: id}});
                    dispatch({type: 'getLineChart', payload: {liveBroadcastId: id}});
                    dispatch({type: 'getBarChart', payload: {liveBroadcastId: id}});
                    // dispatch({type: 'getAreaChart', payload: {liveBroadcastId: id}});
                    // dispatch({type: 'getUserPosition', payload: {liveBroadcastId: id}});
                    dispatch({type: 'getUserPosition2', payload:{id}});


                } // end if
            }) // end listen
        },
    },

    // 方法列表
    effects: {
        * getLiveBaseStatistics({payload}, {put, call, select}) {
            try {
                let {success, result} = yield call(getLiveBaseStatistics, payload)
                if (success) {
                    if (result) {
                        result.liveLength = formatSeconds(result.liveLength);
                        result.watchPersonCount = result.watchPersonCount + '人';
                        result.watchCount = result.watchCount + '次';
                        result.watchAvg = formatSeconds(result.watchAvg);
                    }
                    yield put({
                        type: 'updateStore', payload: {detail: result}
                    })
                }
            } catch (e) {
                console.log(e)
            }
        },
        * getLineChart({payload}, {put, call, select}) {
            let {lineDateList, lineDataList} = yield select(state => state[tmpModule.namespace])
            let {result, success} = yield call(getLineChart, payload)
            if (success) {
                lineDateList = [];
                lineDataList = [];
                if (result) {
                    result.forEach((item) => {
                        lineDateList.push(item.baseStartTime)
                        lineDataList.push(item.watchingPersonCount)
                    });
                }
            }
            yield put({
                type: 'updateStore',
                payload: {
                    lineDateList:lineDateList,
                    lineDataList:lineDataList,
                    loading: true
                }
            })
        },
        * getBarChart({payload}, {put, call, select}) {

            let {barDataList, barDuration} = yield select(state => state[tmpModule.namespace])
            let {result, success} = yield call(getBarChart, payload)
            if (success) {
                barDataList = [];
                barDuration = [];
                if (result) {
                    result.forEach((item) => {
                        barDataList.push(item.personCount)
                        barDuration.push(item.duration)
                    });
                }
            }
            yield put({
                type: 'updateStore',
                payload: {
                    barDataList:barDataList,
                    barDuration:barDuration
                }
            })
        },
        * getAreaChart({payload}, {put, call, select}) {
            let {result, success} = yield call(getAreaChart, payload)
            let geoCoordMap = []
            let cityValueMap = []
            if (success) {
                if (result) {
                    result.forEach((item) => {
                        cityValueMap.push({name: item.cityName, value: item.watchcount});
                        geoCoordMap[item.cityName] = [item.lng, item.lat];
                    });
                }
            }
            yield put({
                type: 'updateStore',
                payload: {
                    geoCoordMap:geoCoordMap,
                    cityValueMap:cityValueMap
                }
            })
        },
        * getUserPosition({payload}, {put, call, select}) {
            let {result, success} = yield call(getUserPosition, payload)
            if(success){
                yield put({
                    type: 'updateStore',
                    payload: {
                        markers:result
                    }
                })
            }
        },
        * getUserPosition2({payload}, {put, call, select}) {
            let {rows, success} = yield call(getUserPosition2, payload)
            if(success){
                yield put({
                    type: 'updateStore',
                    payload: {
                        markers:rows
                    }
                })
            }
        },

    },

    // 存入状态机
    reducers: {
        updateStore(preState, action) {
            return Object.assign({}, preState, action.payload)
        },
    }
}

export default tmpModule
